const { ccclass, property } = cc._decorator;

@ccclass
export default class BackgroundControl extends cc.Component {

  @property
  speed: number = 0;

  @property(cc.Node)
  bg1: cc.Node = null

  @property(cc.Node)
  bg2: cc.Node = null

  start() {
    this.init()
  }

  update(dt: number) {
    this.move(dt)
  }

  /**
   * 背景自动移动函数
   * @param dt update的dt
   */
  move(dt: number) {
    this.bg1.y -= this.speed * dt
    this.bg2.y -= this.speed * dt

    if (this.bg1.y < -this.bg1.height) {
      this.bg1.y += this.bg1.height * 2
    }

    if (this.bg2.y < -this.bg2.height) {
      this.bg2.y += this.bg2.height * 2
    }
  }

  init(): void {
    this.bg1.y = 26 // 26 = 背景高度 - 屏幕高度
    this.bg2.y = this.bg2.height + 26
  }
}
